﻿using System;
using UnityEngine;
using UnityEngine.UIElements;

namespace winS.UnityEditor.UIElement
{
    /// <summary>
    /// 具备侧边栏和对应内容的视图
    /// </summary>
    public class SidebarView : VisualElement
    {
        public event Action<int> onIndexChanged;

        public int index { get => _index; set => sidebar[_index = value].Activate(); }
        public float barWidth { get => sidebar.width; set => sidebar.width = value; }

        public readonly Sidebar sidebar = new Sidebar();
        public readonly VerticalSlideView verticalSlideView = new VerticalSlideView();

        private int _index;

        internal SidebarView()
        {
            style.flexGrow = 1f;
            style.flexDirection = FlexDirection.Row;
            Add(sidebar);
            Add(verticalSlideView);
            sidebar.onIndexChanged += index => onIndexChanged?.Invoke(index);
            verticalSlideView.style.flexGrow = 1f;
            verticalSlideView.style.marginLeft = 2f;
        }

        public void ClearViewContent()
        {
            verticalSlideView.Clear();
        }
        public void AddPage(VisualElement page)
        {
            verticalSlideView.Add(page);
        }
        /// <summary>
        /// 创建一个侧边栏分页
        /// </summary>
        public SidebarButton CreatePage(string name, VisualElement page)
        {
            verticalSlideView.Add(page);
            return sidebar.CreateSidebarButton(name, index => verticalSlideView.SlideContent(_index = index));
        }
        /// <summary>
        /// 创建一个侧边栏分页
        /// </summary>
        public SidebarButton CreatePage(Texture texture, string name, VisualElement page)
        {
            verticalSlideView.Add(page);
            return sidebar.CreateSidebarButton(texture, name, index => verticalSlideView.SlideContent(_index = index));
        }
    }
}
